package com.zh.note.huawei.onehundred.支持优先级的队列;

import java.util.*;

/**
 * https://dream.blog.csdn.net/article/details/131330468
 */
public class Main {
    static class Task implements Comparable<Task> {
        int data;
        int priority;

        public Task(int data, int priority) {
            this.data = data;
            this.priority = priority;
        }

        @Override
        public int compareTo(Task o) {
            // 按照优先级降序排列
            // return o.priority - this.priority;
            return Integer.compare(o.priority, this.priority);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Task task = (Task) o;
            return data == task.data && priority == task.priority;
        }

        @Override
        public int hashCode() {
            return Objects.hash(data, priority);
        }
    }

    public static void main(String[] args) {
        // Scanner sc = new Scanner(System.in);
        // String input = sc.nextLine();
        // sc.close();
        String input = "(10,1),(10,1),(30,2),(40,3)";

        String[] split = input.replaceAll("[()]", "").split(",");
        //PriorityQueue排序规则
        PriorityQueue<Task> taskPriorityQueue = new PriorityQueue<>();

        for (int i = 0; i < split.length; i += 2) {
            int data = Integer.parseInt(split[i]);
            int priority = Integer.parseInt(split[i + 1]);
            taskPriorityQueue.offer(new Task(data, priority));
        }
        List<Integer> res = new ArrayList<>();
        Task preTask = null;

        while (!taskPriorityQueue.isEmpty()) {
            Task curTask = taskPriorityQueue.poll();
            if (preTask == null || !curTask.equals(preTask)) {
                res.add(curTask.data);
            }
            preTask = curTask;
        }
        //list转化成字符串数组
        String[] strings = res.stream().map(String::valueOf).toArray(String[]::new);
        String str = String.join(",", strings);
        System.out.println(str);

    }
}
